/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.map.service.impl;

import me.zhengjie.modules.map.domain.RoutePont;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.map.repository.RoutePontRepository;
import me.zhengjie.modules.map.service.RoutePontService;
import me.zhengjie.modules.map.service.dto.RoutePontDto;
import me.zhengjie.modules.map.service.dto.RoutePontQueryCriteria;
import me.zhengjie.modules.map.service.mapstruct.RoutePontMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2022-06-02
**/
@Service
@RequiredArgsConstructor
public class RoutePontServiceImpl implements RoutePontService {

    private final RoutePontRepository routePontRepository;
    private final RoutePontMapper routePontMapper;

    @Override
    public Map<String,Object> queryAll(RoutePontQueryCriteria criteria, Pageable pageable){
        Page<RoutePont> page = routePontRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(routePontMapper::toDto));
    }

    @Override
    public List<RoutePontDto> queryAll(RoutePontQueryCriteria criteria){
        return routePontMapper.toDto(routePontRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RoutePontDto findById(String id) {
        RoutePont routePont = routePontRepository.findById(id).orElseGet(RoutePont::new);
        ValidationUtil.isNull(routePont.getId(),"RoutePont","id",id);
        return routePontMapper.toDto(routePont);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoutePontDto create(RoutePont resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return routePontMapper.toDto(routePontRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RoutePont resources) {
        RoutePont routePont = routePontRepository.findById(resources.getId()).orElseGet(RoutePont::new);
        ValidationUtil.isNull( routePont.getId(),"RoutePont","id",resources.getId());
        routePont.copy(resources);
        routePontRepository.save(routePont);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            routePontRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RoutePontDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RoutePontDto routePont : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" routeId",  routePont.getRouteId());
            map.put("经度", routePont.getLng());
            map.put(" lat",  routePont.getLat());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public  List<Map<String,String>>  getPoint(Integer routeId) {
        return routePontRepository.getPoint(routeId);
    }

    @Override
    public List<Map<String, String>> getPoint() {
        return routePontRepository.getPoint();
    }
}